package com.smallnico.opencv;

import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;

public class TestOcr {

	public static void main(String[] args) throws IOException, TesseractException {
		
		String path = "C:\\Users\\ainil\\Desktop\\1.png";
		
		File file = new File(path);
		BufferedImage textImage = ImageIO.read(file);
		
		String ws = discernWord(textImage, new Rectangle(0, 0, textImage.getWidth(), textImage.getHeight()));
		System.out.println(ws);
	}
	
	public static String discernWord(BufferedImage image, Rectangle rect) throws IOException, TesseractException {
		Tesseract instance = new Tesseract();
		instance.setDatapath("E:\\Tesseract-OCR-4\\tessdata");// 设置语言库
		instance.setLanguage("chi_sim+eng+osd");// 中文识别
		
//		instance.setTessVariable("chop_enable","T");
//		instance.setTessVariable("use_new_state_cost","F");
//		instance.setTessVariable("segment_segcost_rating","F");
//		instance.setTessVariable("enable_new_segsearch","0");
//		instance.setTessVariable("language_model_ngram_on","0");
//		instance.setTessVariable("textord_force_make_prop_words","F");
		String words = null;
//		words = instance.doOCR(textImage);
		words = instance.doOCR(image, rect);
		
		return words;
	}
}
